package com.qf.service.impl;

import com.fasterxml.jackson.databind.util.BeanUtil;
import com.qf.exception.CustomerException;
import com.qf.mapper.ClassRoomMapper;
import com.qf.mapper.StudentMapper;
import com.qf.pojo.ClassRoom;
import com.qf.pojo.Student;
import com.qf.service.StudentService;
import com.qf.vo.StudentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zdl
 * @date 2025/2/17
 */
@Service
public class StudentServiceImpl implements StudentService {
    @Resource
    private StudentMapper studentMapper;
    @Resource
    private ClassRoomMapper classRoomMapper;

    @Override
    public Student findStudentById(int id) {
        Student student = studentMapper.selectStudentById(id);
        if (student == null) {
            throw new CustomerException("5001", "查询不存在");
        }
        return student;
    }

    @Override
    public List<Student> findAllStudents() {
        return studentMapper.selectAllStudents();
    }

    @Override
    public List<Student> findStudentLikeSnameAndAge(String sname, Integer age) {
        List<Student> students = studentMapper.selectStudentLikeSnameAndAge(sname, age);
        if (students.isEmpty()){
            throw  new CustomerException("20001","暂无数据");
        }
        return students;
    }

    @Override
    public int addStudent(Student student) {
        //插入前 判断 插入数据是否存在 以sname 作为条件
        Student student1 = studentMapper.selectStudentBySname(student.getSname());
        if (student1 != null) {
            throw new CustomerException("50002", "用户名重复");
        }
        //添加判断班级是否存在
        ClassRoom classRoom = classRoomMapper.selectClassRoom(student.getCid());
        if (classRoom == null) {
            throw new CustomerException("60001", "班级不存在");
        }
        return studentMapper.insertStudent(student);
    }

    @Override
    public int updateStudent(Student student) {
        //修改前 查询修改数据是否存在 以id为条件
        Student student1 = studentMapper.selectStudentById(student.getId());
        if (student1 == null) {
            throw new CustomerException("50003", "该用户不存在");
        }
        //若修改班级不存在  不能修改
        ClassRoom classRoom = classRoomMapper.selectClassRoom(student.getCid());
        if (classRoom == null){
            throw new CustomerException("60001", "班级不存在");
        }
        return studentMapper.updateStudent(student);
    }

    @Override
    public int delBatchStudentByIds(List<Integer> id) {
        //删除时 防止拼接错误id  先查询是否存在
        for (Integer integer : id) {
            Student student = studentMapper.selectStudentById(integer);
            if (student == null) {
                throw new CustomerException("50003", "该用户不存在");
            }
        }
        return studentMapper.delBatchStudentByIds(id);
    }

    @Override
    public int delStudentById(Integer id) {
        Student student = studentMapper.selectStudentById(id);
        if (student == null) {
            throw new CustomerException("50003", "该用户不存在");
        }
        return studentMapper.delStudentById(id);
    }

    @Override
    public List<StudentVo> findAllStudentAndClassRoom() {
        return studentMapper.selectAllStudentAndClassRoom();
    }

    @Override
    public StudentVo findStudentAndAllSubjectAndTeacherBySid(Integer id) {
        //判断该学生是否存在
        Student student = studentMapper.selectStudentById(id);
        if (student == null) {
            throw new CustomerException("50003", "该用户不存在");
        }
        return studentMapper.selectStudentAndAllSubjectAndTeacherByStuid(id);
    }
}
